package com.ssm.forum.rpc.service.impl.reply;

import com.github.pagehelper.PageInfo;
import com.ssm.forum.dao.mapper.reply.ReplyDao;
import com.ssm.forum.dao.mapper.residence.ResidenceDao;
import com.ssm.forum.dao.mapper.user.UserDao;
import com.ssm.forum.dao.model.reply.Reply;
import com.ssm.forum.rpc.api.reply.ReplyService;
import com.ssm.icc.dao.mapper.estate.EstateDao;
import com.ssm.upms.dao.mapper.ssm.AdminDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* 帖子回复管理 Service
* @author GaoXiang
* @version 1.0
*/
@Service
public class ReplyServiceImpl implements ReplyService {

    @Resource
    private ReplyDao replyDao;

    @Resource
    private UserDao userDao;

    @Resource
    private ResidenceDao residenceDao;

    @Resource
    private AdminDao adminDao;

/*    @Resource
    private EstateDao estateDao;*/






    /**
    * 保存数据
    * @param reply 实体对象
    * @return 实体id
    * @throws Exception 数据保存异常
    */
    public Integer insert(Reply reply) throws Exception {
        return replyDao.insert(reply);
    }

    /**
    * 根据 id 修改
    * @param reply 带id的实体对象
    * @return 受影响的行数
    * @throws Exception 数据修改异常
    */
    public Integer updateById(Reply reply) throws Exception {
        return replyDao.updateById(reply);
    }

    /**
    * 根据 id 删除
    * @param id 数据id
    * @return 受影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteById(String id) throws Exception {
        return replyDao.deleteById(id);
    }

    /**
    * 根据 id 查找
    * @param id 实体id
    * @return 实体
    */
    public Reply selectById(String id) {
        return replyDao.selectById(id);
    }

    /**
    * 根据 id 批量删除
    * @param ids 要删除的id
    * @return 影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteByIds(List<String> ids) throws Exception {
        return replyDao.deleteByIds(ids);
    }

    /**
    * 查询列表
    * @param map 参数
    * @return 列表
    */
    public PageInfo<Reply> list(Map map) {
        return replyDao.list(map);
    }

    /**
    * 查询列表 带分页
    * @param map 参数
    * @param page 页码
    * @param size 每页大小
    * @return 列表
    */
    public PageInfo<Reply> list(Map map,int page,int size) {
        PageInfo<Reply> replyPageInfo=replyDao.list(map,page,size);;
        for (Reply reply:replyPageInfo.getList()){
            if(reply.getType().equals("1")){
                reply.setAdmin(adminDao.selectById(reply.getUserId()));
            }/*else if (reply.getType().equals("2")){
                reply.setEstate(estateDao.selectById(reply.getUserId()));
            }*/else if(reply.getType().equals("3")){
                reply.setRe(residenceDao.selectById(reply.getUserId()));
            }else if(reply.getType().equals("4")){
                reply.setUser(userDao.selectById(reply.getUserId()));
            }
            reply.setResidence(residenceDao.selectById(reply.getResidenceId()));
        }

        return replyPageInfo;
    }

    /**
     * 根据帖子的Id查询
     * 2017-6-6 10:27:47 韩榕榕
     * @param map
     * @param page
     * @param size
     * @return
     */
    public PageInfo<Reply> selectByContentId(Map map,int page,int size){
        PageInfo<Reply> replyPageInfo=replyDao.selectByContentId(map,page,size);
        for(Reply reply:replyPageInfo.getList()){
            if(reply.getType().equals("1")){
                reply.setAdmin(adminDao.selectById(reply.getUserId()));
            }/*else if (reply.getType().equals("2")){
                reply.setEstate(estateDao.selectById(reply.getUserId()));
            }*/else if(reply.getType().equals("3")){
                reply.setRe(residenceDao.selectById(reply.getUserId()));
            }else if(reply.getType().equals("4")){
                reply.setUser(userDao.selectById(reply.getUserId()));
            }

        }
         return replyDao.selectByContentId(map,page,size);
    }

    /**
     * 根据帖子的ID删除回复
     * 2017-6-7 09:53:46 韩榕榕
     * @param contentId
     * @return
     * @throws Exception
     */
    public Integer deleteByContentId(String contentId) throws Exception {
        return  replyDao.deleteByContentId(contentId);
    }

    /**
     * 根据帖子的ID批量删除回复
     * 2017-6-7 09:54:55 韩榕榕
     * @param contentIds
     * @return
     * @throws Exception
     */
    public  Integer deleteByContentIds(List<String> contentIds) throws Exception {
        return replyDao.deleteByContentIds(contentIds);
    }

}
